﻿Imports System
Imports System.Net.Sockets
Public Class frmMain
    Inherits System.Windows.Forms.Form
    Const sVersion As String = "4.0.0.5"
    Private Config_Info As Config_Class
    Private pltServer() As Pilot_Class
    Private Mission_Info As Mission_Class
    Private Carrier() As CarrierType
    Private CarrierWP() As CarrierWPType
    Private Transport() As Carry 'Transport Columns
    Private FLM() As Marker_Class 'Frontline Marker
    Private AirBase() As Airbase_Class 'Base info
    Private Squad() As Squad_Class 'Squad info
    Private Infantry() As Infant_Class
    Private pltSide() As PilotData_Class 'Pilot side Array
    Private flRed() As Marker_Class
    Private flBlue() As Marker_Class

    Private strRecData(10) As String
    Private MissLine() As String 'Mission lines
    Private sLogInput() As String 'Storage for Eventlog
    Private SupplyAC() As String
    Private LogDestroy() As DestObj
    Private Const sKick As String = " more minutes of kick ban!"
    Private dtPilots As DataTable 'Used for Pilot Grid
    Private dtSquadrons As DataTable 'Used for Squadrons Grid
    Private dtAirBases As DataTable 'Used for Airbase Grid
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = Me.Text & " ver " & sVersion
        Mission_Info.sProgram = CurDir()
        Config_Info.lInter = 15000
        Config_Info.tmAllowGnd = 5
        Config_Info.iErrorTimeout = 5
        Mission_Info.sCfg_File = Mission_Info.sProgram & "\fsscp.cfg"
        Me.gbBridge.Enabled = False
        Me.gbBases.Enabled = False
        If Not My.Computer.FileSystem.FileExists(Mission_Info.sCfg_File) Then
            MsgBox("ERROR - No config file found, check settings")
            With Config_Info
                .sWelcome = "Welcome"
                .sEventLog = "c:\eventlog.lst"
                .sIP = "0.0.0.0"
                .iPort = 2000
                .iMissionLength = 60
                .iDelay = 0
                .iDeath = 0
                .iCapture = 0
                .iBail = 0
                .iRearm = 0
                .iFlight = 4
                .iMaxAC = 8
                .iRadarRange = 10
                .iSupBase = 0
                .iValidFlight = 60
                .iBlockPlt = 1
                .lInter = 15000
                .iBridge = 0
                .iErrorTimeout = 5
            End With
            modMain.WriteCFG(Config_Info, _
                     Mission_Info)
            modMain.ReadCFG(Config_Info, _
                     Mission_Info)
        Else
            ReadCFG(Config_Info, _
                     Mission_Info)
        End If
        modMain.Fill_Form(Config_Info)
        'Mission_Info.tmValid = CDate(Strings.Format(Config_Info.iValidFlight / 24 / 60, "hh:mm:ss tt"))
        FindFolder(Config_Info.sEventLog, Mission_Info.sServer)
    End Sub

    Private Sub cmdAddPilot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddPilot.Click
        Dim sPilot As String
        Dim bFound As Boolean = False
        Dim i As Integer
        sPilot = Trim(Me.tbAddPilot.Text)
        If sPilot <> "" Then
            For i = pltSide.Length - 1 To 1 Step -1
                With pltSide(i)
                    If sPilot = .CallSign Then
                        .Side = Me.nAddPilot.Value
                        bFound = True
                        Exit For
                    End If
                End With
            Next
            If Not bFound Then
                ReDim Preserve pltSide(pltSide.Length)
                With pltSide(pltSide.Length - 1)
                    .CallSign = sPilot
                    .Side = Me.nAddPilot.Value
                    .IP = "0.0.0.0"
                End With
            End If
        End If
    End Sub

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Dim sMessage As String = (tbSendData.Text & vbCrLf)
        TCPSend(Config_Info, sMessage)
    End Sub

    Private Sub nDelay_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nDelay.ValueChanged
        Config_Info.iDelay = sender.value.ToString
        tbTimeLeft.Text = Format(Config_Info.iDelay + Config_Info.iMissionLength, "0")
    End Sub

    Private Sub nMission_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nMission.ValueChanged
        If Me.GroupBox1.Enabled Then
            Config_Info.iMissionLength = sender.value.ToString
            tbTimeLeft.Text = Format(Config_Info.iDelay + Config_Info.iMissionLength, "0")
        End If
    End Sub

    Private Sub nValidFlight_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nValidFlight.ValueChanged
        Config_Info.iValidFlight = sender.value.ToString
    End Sub

    Private Sub nKicks_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nKicks.ValueChanged
        Config_Info.iKicksAllowed = sender.value.ToString
    End Sub

    Private Sub nDeaths_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nDeath.ValueChanged
        Config_Info.iDeath = sender.value.ToString
    End Sub

    Private Sub nCapture_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nCapture.ValueChanged
        Config_Info.iCapture = sender.value
    End Sub

    Private Sub nBail_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nBail.ValueChanged
        Config_Info.iBail = sender.value.ToString
    End Sub

    Private Sub nRearm_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nRearm.ValueChanged
        Config_Info.iRearm = sender.value.ToString
    End Sub

    Private Sub cbBlock_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBlock.CheckedChanged
        If sender.checked Then
            Config_Info.iBlockPlt = 1
        Else
            Config_Info.iBlockPlt = 0
        End If
    End Sub

    Private Sub cbWritePlts_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbWritePlt.CheckedChanged
        If sender.checked Then
            Config_Info.iWritePltFile = 1
        Else
            Config_Info.iWritePltFile = 0
        End If
    End Sub

    Private Sub tbEventlog_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbEventlog.TextChanged
        Config_Info.sEventLog = sender.text
    End Sub

    Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
        With OpenFileDialog1
            .Filter = "eventlog|*.lst"
            .Title = "Select Eventlog File"
            .FileName = Config_Info.sEventLog.ToString
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Config_Info.sEventLog = .FileName
                tbEventlog.Text = .FileName
                FindFolder(Config_Info.sEventLog, Mission_Info.sServer)
            End If
        End With
    End Sub

    Private Sub nradarRange_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nradarRange.ValueChanged
        Config_Info.iRadarRange = sender.value
    End Sub

    Private Sub cbBridge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBridge.CheckedChanged
        If sender.checked Then
            Config_Info.iBridge = 1
        Else
            Config_Info.iBridge = 0
        End If
    End Sub

    Private Sub rbSupplyBases_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSupplyBases.CheckedChanged
        If sender.checked Then
            Config_Info.iSupBase = 1
        Else
            Config_Info.iSupBase = 0
        End If
    End Sub

    Private Sub rbAllBases_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbAllBases.CheckedChanged
        If sender.checked Then
            Config_Info.iSupBase = 0
        Else
            Config_Info.iSupBase = 1
        End If
    End Sub

    Private Sub tbIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbIP.TextChanged
        Config_Info.sIP = sender.text
    End Sub

    Private Sub tbPort_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbPort.TextChanged
        Config_Info.iPort = Val(tbPort.Text)
    End Sub

    Private Sub tbWelcome_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbWelcome.TextChanged
        Config_Info.sWelcome = sender.text
    End Sub

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click

        Dim intCount As Short
        ReDim AirBase(0)
        ReDim Squad(0)
        ReDim pltServer(0)
        ReDim pltSide(0)
        ReDim Carrier(0)
        ReDim CarrierWP(0)
        ReDim Transport(0)
        ReDim FLM(0)
        ReDim Infantry(0)
        ReDim flRed(0)
        ReDim flBlue(0)
        ReDim MissLine(0)
        ReDim sLogInput(0)
        ReDim SupplyAC(0)
        ReDim LogDestroy(0)

        dtPilots = New DataTable
        modMain.PilotDataTable(dtPilots)
        Me.grdPilot.DataSource = dtPilots
        dtSquadrons = New DataTable
        modMain.SquadDataTable(dtSquadrons)
        Me.grdSquadrons.DataSource = dtSquadrons
        dtAirBases = New DataTable
        modMain.BaseDataTable(dtAirBases)
        Me.grdBases.DataSource = dtAirBases
        Mission_Info.dbMissStartTime = 0
        Mission_Info.bMissEnd = False
        Me.cbDebug.Visible = False
        ReDim sLogInput(1)
        ' Find Eventlog
        If Not My.Computer.FileSystem.FileExists(Config_Info.sEventLog) Then
            MsgBox("ERROR - Event Log File not found!")
            Exit Sub
        End If
        ' Find Mission
        Mission_Info.sMission = ""
        FindMission(Config_Info, _
                    Mission_Info, _
                    sLogInput)
        If Mission_Info.sMission = "" Then
            MsgBox("ERROR - Mission File not found in Event Log!")
            Exit Sub
        End If
        If Not My.Computer.FileSystem.FileExists(Mission_Info.sMission) Then
            MsgBox("ERROR - Mission File not found!" & vbCrLf & Mission_Info.sMission)
            Exit Sub
        End If
        LoadMission(Mission_Info, _
                     MissLine, _
                     FLM)
        If Config_Info.iDebug > 0 Then MsgBox("Loaded Mission")
        Mission_Info.sPropFile = Replace(Mission_Info.sMission, ".mis", ".properties")
        If Not My.Computer.FileSystem.FileExists(Mission_Info.sPropFile) Then
            MsgBox("ERROR - Properties File not found!" & vbCrLf & Mission_Info.sPropFile)
            Exit Sub
        End If
        modMain.LoadBases(AirBase, _
                            Squad, _
                            Mission_Info, _
                            Config_Info, _
                            dtSquadrons, _
                            dtAirBases)
        If AirBase.Length < 2 Then
            MsgBox("ERROR - No Bases Found in Properties File!" & vbCrLf & Mission_Info.sPropFile)
            Exit Sub
        End If
        If Config_Info.iDebug > 0 Then MsgBox("Loaded Bases")
        modMain.LoadWeapons(Mission_Info, _
                            Squad)
        If Config_Info.iDebug > 0 Then MsgBox("Loaded Weapons")
        modMain.LoadSupply(Mission_Info, _
                           SupplyAC)
        If Config_Info.iDebug > 0 Then MsgBox("Loaded Supply")
        modMain.Load_Transports(Mission_Info, _
                                Transport)
        If Config_Info.iDebug > 0 Then MsgBox("Loaded Transports")

        modMain.Load_Infantry(Mission_Info, _
                              Infantry, _
                              Config_Info)


        If Config_Info.iDebug > 0 Then MsgBox("Updated Squads")

        FindOffset(Mission_Info)
        If Config_Info.iDebug > 0 Then MsgBox("Found Offset")
        FindFolder(Mission_Info.sMission, _
                   Mission_Info.sMissDir)
        If Config_Info.iDebug > 0 Then MsgBox("Found Folder")
        'Load Carriers
        modMain.LoadCarriers(AirBase, _
                            MissLine, _
                            Carrier, _
                            CarrierWP)
        'Find Number of Seats available on each side
        FindServerSize(Mission_Info.sServer, _
                       Mission_Info.iSeats)
        If Config_Info.iDebug > 0 Then MsgBox("Found Server Size")
        If Mission_Info.iSeats > 0 Then
            Mission_Info.iSeats = Int(Mission_Info.iSeats / 2)
        Else
            MsgBox("ERROR - Cannot find number of seats available!")
            Exit Sub
        End If
        Mission_Info.iRedSide = 0
        Mission_Info.iBlueSide = 0
        'Load Pilot Side Data
        Mission_Info.sPilotSideFile = Mission_Info.sMission
        For intCount = Len(Mission_Info.sPilotSideFile) To 1 Step -1
            If Mid(Mission_Info.sPilotSideFile, intCount, 1) = "\" Or Mid(Mission_Info.sPilotSideFile, intCount, 1) = "/" Then Exit For
        Next
        If intCount > 1 Then
            Mission_Info.sPilotSideFile = Strings.Left(Mission_Info.sPilotSideFile, intCount) & "pilotlist.cfg"
            LoadPilotSide(Mission_Info.sPilotSideFile, _
                          pltSide)
        Else
            Config_Info.iBlockPlt = 0
        End If
        If Config_Info.iDebug > 0 Then MsgBox("Pilot Side Data Loaded")

        If Config_Info.iDebug > 0 Then MsgBox("Loaded Admins")

        ' connection.
        
        modMain.TCPConnect(Config_Info)

        ' Log File Setup
        cmdConnect.Enabled = False
        cmdsave.Enabled = False
        Me.GroupBox1.Enabled = False
        Me.GroupBox2.Enabled = False
        Me.GroupBox3.Enabled = False
        Me.GroupBox4.Enabled = False
        Me.GroupBox5.Enabled = False
        Me.GroupBox6.Enabled = True
        Me.GroupBox7.Enabled = True
        'ACGrid
        Mission_Info.lLogLine = 1
        Mission_Info.lLogSize = 1
        Mission_Info.iTimeLeft = Config_Info.iMissionLength + Config_Info.iDelay
        Mission_Info.iDelayLeft = Config_Info.iDelay
        tbTimeLeft.Text = Format(Mission_Info.iTimeLeft, "0")

        If Config_Info.iDebug > 0 Then MsgBox("Log File Setup")
        ' Load Front Line Markers
        modMain.FrontLine(Mission_Info.sMission, _
                          flRed, _
                          flBlue)
        If Config_Info.iDebug > 0 Then MsgBox("Loaded FLM")
        'Start Timer
        Me.tmrInter.Interval = Config_Info.lInter
        If Config_Info.iDebug > 0 Then
            MsgBox("Timer Started")
            modMain.TimerInter("Time Of Day: 12.0", _
                                    Mission_Info, _
                                     Config_Info, _
                                     pltServer, _
                                     sLogInput, _
                                     Transport, _
                                     AirBase, _
                                     pltSide, _
                                     LogDestroy, _
                                     Squad, _
                                     Infantry, _
                                     SupplyAC, _
                                     MissLine, _
                                     flRed, _
                                     flBlue, _
                                     Carrier, _
                                     CarrierWP, _
                                     FLM, _
                                     dtPilots, _
                                     dtSquadrons, _
                                     sVersion)
            modMain.StopServer(Config_Info, _
                        pltServer, _
                        Mission_Info, _
                        sLogInput, _
                        Transport, _
                        AirBase, _
                        FLM, _
                        sVersion)
        Else
            Me.tmrInter.Enabled = True
        End If
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Beep()
        If vbOK = MsgBox("You are about to close this program!", MsgBoxStyle.OkCancel) Then
            End
        End If
    End Sub

    Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
        WriteCFG(Config_Info, _
                 Mission_Info)
    End Sub

    Private Sub cbDebug_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender.checked Then
            Config_Info.iDebug = 1
        Else
            Config_Info.iDebug = 0
        End If
    End Sub

    Private Sub cmdChangeTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChangeTime.Click
        If Mission_Info.iTimeLeft > 5 And Mission_Info.iTimeLeft + Me.nChangeTime.Value > 5 Then
            Config_Info.iMissionLength += Me.nChangeTime.Value
            Me.nMission.Value = Config_Info.iMissionLength
            Mission_Info.iTimeLeft += Me.nChangeTime.Value
            Me.tbTimeLeft.Text = Str(Mission_Info.iTimeLeft)
        End If
    End Sub

    Private Sub tmrInter_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrInter.Tick
        If Me.cbDebug.CheckState > 0 Then
            modMain.TimerInter("Time Of Day: 12.0", _
                            Mission_Info, _
                            Config_Info, _
                            pltServer, _
                            sLogInput, _
                            Transport, _
                            AirBase, _
                            pltSide, _
                            LogDestroy, _
                            Squad, _
                            Infantry, _
                            SupplyAC, _
                            MissLine, _
                            flRed, _
                            flBlue, _
                            Carrier, _
                            CarrierWP, _
                            FLM, _
                            dtPilots, _
                            dtSquadrons, _
                            sVersion)
            Mission_Info.bMissEnd = True
            modMain.StopServer(Config_Info, _
                        pltServer, _
                        Mission_Info, _
                        sLogInput, _
                        Transport, _
                        AirBase, _
                        FLM, _
                        sVersion)
            Exit Sub
        End If
        If Mission_Info.iWaiting > Config_Info.iErrorTimeout Then 'Server frozen
            modMain.StopServer(Config_Info, _
                        pltServer, _
                        Mission_Info, _
                        sLogInput, _
                        Transport, _
                        AirBase, _
                        FLM, _
                        sVersion)
        Else
            modMain.TCPSend(Config_Info, "tod" & vbCrLf)
            Mission_Info.iWaiting += 1
            Me.lErrorCount.Text = Mission_Info.iWaiting.ToString
        End If
    End Sub

    Private Sub tmrTCP_Read_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTCP_Read.Tick
        Dim iLine As Integer
        Dim sData As String = ""
        TCPReceive(Config_Info, _
                        sData)
        If sData.Length > 1 Then
            Dim sLines() As String
            Dim stringSeparators() As String = {"\n"}

            sLines = sData.Split(stringSeparators, StringSplitOptions.None)
            Dim sline As String
            For Each sline In sLines
                tbRecData.Text = ""
                For iLine = 0 To 9 Step 1
                    strRecData(iLine) = strRecData(iLine + 1)
                    tbRecData.Text &= strRecData(iLine) & vbCr
                Next
                strRecData(10) = sline
                tbRecData.Text &= strRecData(10)
                'If InStr(strData, "has beed captured by the") > 0 Then
                '    Capt strData
                'End If
                If InStr(sline, "<status") > 0 Then
                    modMain.Status(sline, _
                                   pltServer, _
                                   Mission_Info, _
                                   Config_Info)
                End If
                If InStr(sline, "<help") > 0 Then
                    modMain.Help(sline, _
                                 Config_Info)
                End If
                If InStr(sline, "<aircraft") > 0 Then
                    modMain.Aircraft(sline, _
                                     pltServer, _
                                     Squad, _
                                     Config_Info)
                End If
                If InStr(sline, "<timeleft") > 0 Then
                    modMain.TimeLeft(sline, _
                                     Mission_Info.iTimeLeft, _
                                     Config_Info)
                End If
                If InStr(sline, "<settings") > 0 Then
                    modMain.Settings(sline, _
                                     Config_Info)
                End If
                If InStr(sline, "<ver") > 0 Then
                    modMain.Version(sline, _
                                    Config_Info)
                End If

                If InStr(sline, "<home") > 0 Then
                    modMain.Home(sline, _
                                 pltServer, _
                                 Config_Info)
                End If

                If InStr(sline, "<sides") > 0 Then
                    modMain.Sides(sline, _
                                  Mission_Info, _
                                  Config_Info)
                End If
                'If InStr(strData, "<<kick") > 0 Then
                '    modMain.ReqKick strData
                'End If
                'If InStr(strData, "<<ban") > 0 Then
                '    modMain.ReqBan strData
                'End If
                If InStr(sline, "Time Of Day:") > 0 Then
                    modMain.TimerInter(sline, _
                                        Mission_Info, _
                                         Config_Info, _
                                         pltServer, _
                                         sLogInput, _
                                         Transport, _
                                         AirBase, _
                                         pltSide, _
                                         LogDestroy, _
                                         Squad, _
                                         Infantry, _
                                         SupplyAC, _
                                         MissLine, _
                                         flRed, _
                                         flBlue, _
                                         Carrier, _
                                         CarrierWP, _
                                         FLM, _
                                         dtPilots, _
                                         dtSquadrons, _
                                         sVersion)
                End If
                If InStr(sline, "<load_troops") > 0 Then
                    modMain.Load_Troops_on_AC(sline, _
                                  pltServer, _
                                  SupplyAC, _
                                  Infantry, _
                                  Config_Info)
                End If
                If InStr(sline, "socket channel") > 0 And InStr(sline, "is complete") > 0 Then
                    modMain.FindIP(sline, _
                                   pltSide)
                End If
            Next
        End If
    End Sub

    Private Sub nErrorTimeout_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nErrorTimeout.ValueChanged
        Config_Info.iErrorTimeout = sender.value.ToString
    End Sub
End Class
